package Engine.PhysicEngine;

import Engine.*;
import Engine.World.World;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.lwjgl.Sys;
import org.newdawn.slick.util.BufferedImageUtil;
import org.newdawn.slick.util.ResourceLoader;

public class Physic extends Thread {

	private java.util.List<PhysicObject> objects;

	private World terrain;

	public Physic(Engine engine) {

	}

	public void AddObject(PhysicObject obj) {

	}

	public int getRGB(float x, float y, RigidBody rigidbody) {
		x += 0;
		y += 0;
		x /= 16;
		y /= 16;
		//System.out.println("X: " + (int) x + " Y: " + (int) y);
		try {
			if (terrain.getBlockAt((int) x, (int) y).ID() == 0) {
				return 0;
			} else if (terrain.getBlockAt((int) x, (int) y).ID() == -1) {
				return -65536;

			} else if (terrain.getBlockAt((int) x, (int) y).ID() == 1) {
				//System.out.print("Dirt");
				return -65536;
			}else if (terrain.getBlockAt((int) x, (int) y).ID() == 10) {
				//System.out.print("Dirt");
				return -16776961;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return 0;
	}

	public float lava = 0.2f;
	public float water = 0.1f;

	public float InteractX(BoxCollider collider) {
		int x = collider.Xpos;
		for (int y = collider.Ypos; y < collider.Height + collider.Ypos; y++) {

			if (getRGB((int) (x), (int) (y), collider.rigidbody) == -65536)
				return 0;
			if (getRGB((int) (x + collider.Width), (int) y, collider.rigidbody) == -65536)
				return 0;

		}
		for (int y = collider.Ypos; y < collider.Height + collider.Ypos; y++) {

			if (getRGB((int) (x), (int) (y), collider.rigidbody) == -16776961)
				return lava;
			if (getRGB((int) (x + collider.Width), (int) y, collider.rigidbody) == -16776961)
				return lava;
		}
		return 1;
	}

	public float InteractY(BoxCollider collider) {
		int y = collider.Ypos;
		for (int x = collider.Xpos; x < collider.Width + collider.Xpos; x++) {

			if (getRGB((int) (x), (int) (y), collider.rigidbody) == -65536)
				return 0;
			if (getRGB((int) (x), (int) (y + collider.Height), collider.rigidbody) == -65536)
				return 0;

		}
		for (int x = collider.Xpos; x < collider.Width + collider.Xpos; x++) {
			if (getRGB((int) (x), (int) (y), collider.rigidbody) == -16776961)
				return lava;
			if (getRGB((int) (x), (int) (y + collider.Height), collider.rigidbody) == -16776961)
				return lava;
		}
		return 1;
	}

	public boolean IsGrounded(BoxCollider collider) {
		int y = collider.Ypos;
		for (int x = collider.Xpos; x < collider.Width + collider.Xpos; x++) {

			if (getRGB((int) (x), (int) (y + collider.Height), collider.rigidbody) == -65536)
				return true;

		}
		for (int x = collider.Xpos; x < collider.Width + collider.Xpos; x++) {

			if (getRGB((int) (x), (int) (y + collider.Height), collider.rigidbody) == -16776961)
				return true;
		}
		return false;
	}

	public float CanJump(BoxCollider collider) {
		int y = collider.Ypos;
		for (int x = collider.Xpos; x < collider.Width + collider.Xpos; x++) {

			Color color = new Color(getRGB((int) (x), (int) (y
					+ collider.Height + 1), collider.rigidbody));
			if (color.getRed() == 100)
				return (color.getBlue() / 10) + 1;
			if (getRGB((int) (x), (int) (y + collider.Height + 1), collider.rigidbody) == -65536)
				return 1;
			if (getRGB((int) (x), (int) (y + collider.Height), collider.rigidbody) == -16776961) {
				if (getRGB((int) (x), (int) (y + collider.Height - 3), collider.rigidbody) == -16776961)
					return lava;
				return 0;
			}

		}
		return 0;
	}

	public World getTerrain() {
		return terrain;
	}

	public void setTerrain(World terrain) {
		this.terrain = terrain;
	}
}
